<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class Ceshi extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'ceshi';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'ceshi_enum_text',
        'ceshi_set_text',
        'phone_model_list_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            if (!$row['weigh']) {
                $pk = $row->getPk();
                $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
            }
        });
    }

    
    public function getCeshiEnumList()
    {
        return ['美国' => __('美国'), '中国' => __('中国'), '韩国' => __('韩国')];
    }

    public function getCeshiSetList()
    {
        return ['苹果' => __('苹果'), '橘子' => __('橘子'), '香蕉' => __('香蕉'), '李子' => __('李子')];
    }

    public function getPhoneModelListList()
    {
        return ['苹果' => __('苹果'), '小米' => __('小米'), '华为' => __('华为'), 'VIVO' => __('VIVO'), 'OPPO' => __('OPPO')];
    }


    public function getCeshiEnumTextAttr($value, $data)
    {
        $value = $value ?: ($data['ceshi_enum'] ?? '');
        $list = $this->getCeshiEnumList();
        return $list[$value] ?? '';
    }


    public function getCeshiSetTextAttr($value, $data)
    {
        $value = $value ?: ($data['ceshi_set'] ?? '');
        $valueArr = explode(',', $value);
        $list = $this->getCeshiSetList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }


    public function getPhoneModelListTextAttr($value, $data)
    {
        $value = $value ?: ($data['phone_model_list'] ?? '');
        $list = $this->getPhoneModelListList();
        return $list[$value] ?? '';
    }

    protected function setCeshiSetAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }


}
